//
//  CACoinCell.m
//  cascade-realm
//
//  Created by QSC on 29/5/24.
//

#import "CACoinCell.h"
#import "CAGoodsModel.h"
#import "UIImage+Extension.h"
@interface CACoinCell()
@property(nonatomic,strong)UILabel * numLabel;
@property(nonatomic,strong)UILabel * discountLabel;
@property(nonatomic,strong)UILabel * priceLabel;
@property(nonatomic,strong)UILabel * originalPriceLabel;
@property(nonatomic,strong)UIImageView * coinImagev;

@end
@implementation CACoinCell
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame: frame]) {
        [self setUI];
    }
    return  self;
}

- (void)setUI{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 8;
    [self.contentView setDefultBorder];

    
    UIImageView * coinImg = [[UIImageView alloc] initWithImage:Img(@"coins")];
    coinImg.contentMode = UIViewContentModeScaleAspectFit;
    self.coinImagev = coinImg;
    [self.contentView addSubview:coinImg];
    [coinImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(12);
        make.size.mas_equalTo(44);
    }];
    
    UILabel * numLabel = [UILabel LabelWithFont:Font(20) color:Hex(0x565656) text:@""];
    self.numLabel = numLabel;
    [self.contentView addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(coinImg.mas_right).offset(-22);
        make.top.equalTo(coinImg.mas_bottom).offset(12);
    }];
    
    UILabel * discountLabel = [UILabel LabelWithFont:Font(9) color:Hex(0xF81768) text:@""];
    self.discountLabel = discountLabel;
    [self.contentView addSubview:discountLabel];
    [discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numLabel.mas_right).offset(8);
        make.bottom.equalTo(numLabel);
    }];
    
    UIView * bottomView = [UIView new];
    bottomView.backgroundColor = Hex(0xE4F86E);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [bottomView addRoundedCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight  withRadii: CGSizeMake(8,8)];
    });

    [self.contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(36);
    }];
    
  
    
 
    
    UILabel * priceLabel = [UILabel LabelWithFont:BoldFont(14) color:Hex(0x11660A) text:@""];
    self.priceLabel = priceLabel;
    [bottomView addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView);
        make.centerY.equalTo(bottomView);
    }];
    
    UILabel * originalPriceLabel = [UILabel LabelWithFont:Font(10) color:[[UIColor blackColor] colorWithAlphaComponent:0.5] text:@""];
    self.originalPriceLabel = originalPriceLabel;
    [bottomView addSubview:originalPriceLabel];
    [originalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomView);
        make.left.equalTo(priceLabel.mas_right).offset(8);
    }];
    
}





- (void)setModel:(CAGoodsModel *)model{
    _model = model;
    self.numLabel.text = model.exchangeCoin;
    self.discountLabel.text = [NSString stringWithFormat:@"%ld%% off",(long)(model.discount * 100)];
    self.priceLabel.text = [NSString stringWithFormat:@"$%.2f",model.price];
    self.originalPriceLabel.text = [NSString stringWithFormat:@"$%.2f",model.originalPrice];
    
    if (model.discount == 0) {
        self.discountLabel.hidden = true;
        self.originalPriceLabel.hidden = true;
        [self.numLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.top.equalTo(self.coinImagev.mas_bottom).offset(12);
        }];
        
    }else{
        self.discountLabel.hidden = false;
        self.originalPriceLabel.hidden = false;
        [self.numLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.coinImagev.mas_right).offset(-22);
            make.top.equalTo(self.coinImagev.mas_bottom).offset(12);
        }];
    }
    
   
}



@end
